import 'package:shared_preferences/shared_preferences.dart';
import '../../core/api_response.dart';
import '../app_state_repository.dart';
import '../../services/mock_api_service.dart';

class MockAppStateRepository implements AppStateRepository {
  @override
  Future<ApiResponse<DateTime>> getRelationshipStartDate() async {
    return MockApiService.simulateApiCall<DateTime>(() async {
      final prefs = await SharedPreferences.getInstance();
      final startDateString = prefs.getString('relationship_start_date');
      
      if (startDateString != null) {
        return DateTime.parse(startDateString);
      }
      
      return DateTime.now();
    });
  }
  
  @override
  Future<ApiResponse<String>> getPartnerName1() async {
    return MockApiService.simulateApiCall<String>(() async {
      final prefs = await SharedPreferences.getInstance();
      return prefs.getString('partner_name_1') ?? '👩';
    });
  }
  
  @override
  Future<ApiResponse<String>> getPartnerName2() async {
    return MockApiService.simulateApiCall<String>(() async {
      final prefs = await SharedPreferences.getInstance();
      return prefs.getString('partner_name_2') ?? '👨';
    });
  }
  
  @override
  Future<ApiResponse<void>> setRelationshipStartDate(DateTime date) async {
    return MockApiService.simulateVoidApiCall(() async {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setString('relationship_start_date', date.toIso8601String());
    });
  }
  
  @override
  Future<ApiResponse<void>> setPartnerNames(String name1, String name2) async {
    return MockApiService.simulateVoidApiCall(() async {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setString('partner_name_1', name1);
      await prefs.setString('partner_name_2', name2);
    });
  }
  
  @override
  Future<ApiResponse<int>> getDaysInRelationship() async {
    return MockApiService.simulateApiCall<int>(() async {
      final startDateResponse = await getRelationshipStartDate();
      if (!startDateResponse.isSuccess || startDateResponse.data == null) {
        throw Exception('获取恋爱开始日期失败');
      }
      
      final startDate = startDateResponse.data!;
      return DateTime.now().difference(startDate).inDays + 1;
    });
  }
}